package com.wh.wisdomsite.xbox.server.filter;

import org.apache.log4j.PropertyConfigurator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class Log4jInit extends HttpServlet {

	private static final long serialVersionUID = 8733237315512854070L;

    private static final Log logger = LogFactory.getLog(Log4jInit.class);


    /** 该属性用来指定是否设置web应用根目录环境变量。 */
    public static final String EXPOSE_WEB_APP_ROOT_PARAM = "log4jExposeWebAppRoot";

    /** 该属性用来指定log4j 配置文件的路径。 */
    public static final String CONFIG_LOCATION_PARAM = "log4jConfigLocation";

    /** 该属性用来指定检查log4j 配置文件的间隔时间。 */
    public static final String REFRESH_INTERVAL_PARAM = "log4jRefreshInterval";

    private final String WEB_APP_ROOT_DEFAULT = "webapp.root";

    /**
	 * <li>HttpServlet的初始化方法</li>
	 * <li>调用该方法来初始化Log4j日志系统</li>
	 */
	public void init() {
        Integer server = 2;
        switch (server){
            //1. apache tomcat use
            case 1:
                String prefix = getServletContext().getRealPath("/");
                String file = getInitParameter("log4j");
                logger.info("log4j apache tomcat 开始");
                logger.info("log4j prefix : " + prefix);
                logger.info("log4j file : " + file);
                logger.info("log4j 初始化开始");
                if(file != null) {
                    logger.info("log4j 初始化成功");
                    PropertyConfigurator.configure(prefix + file);
                }else{
                    logger.info("log4j 初始化失败");
                }
                logger.info("log4j 初始化结束");
                logger.info("log4j apache tomcat 结束");
                break;
            //2. weblogic use
            case 2:
                String prefix_weblogic = getServletContext().getRealPath("/");
                String webAppRootKey = getServletConfig().getInitParameter("webAppRootKey");
                if(webAppRootKey == null || webAppRootKey.length() < 1){
                    webAppRootKey = WEB_APP_ROOT_DEFAULT;
                }
                System.setProperty(webAppRootKey, prefix_weblogic);
                String propfile_weblogic = getInitParameter("log4jConfigLocation");
                logger.info("log4j weblogic 开始");
                logger.info("log4j realPath: " + prefix_weblogic);
                logger.info("log4j logCfgFile: " + propfile_weblogic);
                logger.info("log4j 初始化开始");
                if(null != propfile_weblogic){
                    try{
                        PropertyConfigurator.configure(prefix_weblogic + propfile_weblogic);
                        //ps.load(getServletContext().getResourceAsStream(prefix_weblogic + propfile_weblogic));
                        logger.info("log4j 初始化成功");
                    }catch(Exception e){
                        logger.info("log4j 初始化失败");
                        e.printStackTrace();
                    }
                }
                logger.info("log4j 初始化结束");
                logger.info("log4j weblogic 结束");
                break;
        }
    }






    /**
     * 返回是否进行web应用根目录系统属性设置。
     */
//    private static boolean exposeWebAppRoot(ServletContext servletContext) {
//        String exposeWebAppRootParam = servletContext.getInitParameter(EXPOSE_WEB_APP_ROOT_PARAM);
//        return (exposeWebAppRootParam == null || Boolean.valueOf(exposeWebAppRootParam));
//
//    }

    /**
     * 根据相对路径得到web应用下的绝对路径
     */
//    public static String getRealPath(ServletContext servletContext, String path) throws FileNotFoundException {
//        Assert.notNull(servletContext, "ServletContext must not be null");
//        if (!path.startsWith("/")) {
//            path = "/" + path;
//        }
//        String realPath = servletContext.getRealPath(path);
//        if (realPath == null) {
//            throw new FileNotFoundException("ServletContext resource [" + path + "] cannot be resolved to absolute file path - " +"web application archive not expanded?");
//        }
//        return realPath;
//
//    }





	/**
	 * <li>HttpServlet的doGet方法</li>
	 */
	public void doGet(HttpServletRequest req, HttpServletResponse res) {

	}
}
